#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2018-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     have_cgal
#
# Description
#     Detection/setup of CGAL
#
# Requires
#     CGAL_ARCH_PATH
# or  config.sh/CGAL (when CGAL_ARCH_PATH is empty)
#
# Functions provided
#     have_cgal, no_cgal, echo_cgal, query_cgal
#
# Variables set on success
#     HAVE_CGAL
#     CGAL_FLAVOUR (header, library, ...)
#     CGAL_ARCH_PATH
#     CGAL_INC_DIR
#     CGAL_LIB_DIR
#
# Uses
#     BOOST_ARCH_PATH and have_boost::search_boost()
#
#------------------------------------------------------------------------------
. ${WM_PROJECT_DIR:?}/wmake/scripts/have_boost      # boost + system functions

#------------------------------------------------------------------------------

# Reset
no_cgal()
{
    unset HAVE_CGAL CGAL_FLAVOUR CGAL_INC_DIR CGAL_LIB_DIR
}


# Report
echo_cgal()
{
    echo "cgal=${HAVE_CGAL:-false}"
    echo "flavour=$CGAL_FLAVOUR"
    echo "root=\"$CGAL_ARCH_PATH\""
    echo "include=\"$CGAL_INC_DIR\""
    echo "library=\"$CGAL_LIB_DIR\""
}


# Search
# $1 : prefix (*_ARCH_PATH, system, ...)
#
# On success, return 0 and export variables
# -> HAVE_CGAL, CGAL_INC_DIR, CGAL_LIB_DIR, CGAL_FLAVOUR
search_cgal()
{
    local warn # warn="==> skip cgal"
    local incName="CGAL/version.h"
    local libName="libCGAL"

    local prefix="${1:-system}"
    local header library flavour

    # ----------------------------------
    if isNone "$prefix"
    then
        [ -n "$warn" ] && echo "$warn (disabled)"
        return 1
    elif hasAbsdir "$prefix"
    then
        header=$(findFirstFile "$prefix/include/$incName")
        library=$(findExtLib "$libName")
    elif isSystem "$prefix"
    then
        header=$(findSystemInclude -name="$incName")
        prefix=$(sysPrefix "$header")
    else
        unset prefix
    fi
    # ----------------------------------

    # Header
    [ -n "$header" ] || {
        [ -n "$warn" ] && echo "$warn (no header)"
        return 2
    }

    # Library may be optional - eg, header-only
    [ -n "$library" ] \
    || library=$(findLibrary -prefix="$prefix" -name="$libName") \
    || {
        [ -n "$warn" ] && echo "==> cgal (no library)"
        unset library
        flavour="header"    # Header only
    }

    # ----------------------------------

    header="${header%/*}"   # Strip one-level (include/CGAL/...)

    # OK
    export HAVE_CGAL=true
    export CGAL_FLAVOUR="${flavour:-library}"
    export CGAL_ARCH_PATH="$prefix"
    export CGAL_INC_DIR="${header%/*}"     # Basename
    export CGAL_LIB_DIR="${library%/*}"    # Basename
}


# Output as per search_* function
have_cgal()
{
    local warn # warn="==> skip cgal"
    local config="config.sh/CGAL"
    local file

    # Setup - current environment if set
    if [ -z "$CGAL_ARCH_PATH" ]
    then
        if file="$("$WM_PROJECT_DIR"/bin/foamEtcFile "$config")"
        then
            . "$file"
        else
            [ -n "$warn" ] && echo "$warn (no $config)"
            return 2
        fi
    fi

    # Need boost for cgal
    search_boost "$BOOST_ARCH_PATH"

    search_cgal "$CGAL_ARCH_PATH"
}


# Query settings (from CGAL setup)
query_cgal()
{
    local config="config.sh/CGAL"
    local file

    if file="$("$WM_PROJECT_DIR"/bin/foamEtcFile -mode=o "$config")"
    then
        . "$file"
        _process_query cgal "$CGAL_ARCH_PATH"
    else
        echo "(no $config)" 1>&2
        echo "cgal=unknown"
    fi
}


#------------------------------------------------------------------------------

# Reset
no_cgal

# Test/query
case "$1" in
-test)
    have_cgal
    echo_cgal
    ;;
-query)
    query_cgal
    ;;
esac

#------------------------------------------------------------------------------
